define(["../common"], function() {
	
	var paging = {};
	var sorting = {'sorts[0].field': 'spendTime'};
	var $list = $(".expense-list");
	var $loadTip = $(".mr a");
	
	loadData();
	
	// 加载更多
	$loadTip.click(function() {
		if (!$loadTip.isLocked() && paging.nowPage < paging.totalPage) {
			paging.nowPage = paging.nowPage + 1;
			loadData();
		}
	});
	
	function loadData() {
		if (!$loadTip.lock()) return;
		$loadTip.show().html("正在加载...");
		
		var searching = paging;
		for (var key in sorting) searching[key] = sorting[key];
		
		$.getJSON(g_userDomain + "/memberSpend/list/json", searching, function(data){
			$loadTip.unlock();
			$loadTip.html("点击加载更多");
			if (data.status !== 0) {
				$loadTip.infoMsg(data.message);
				return;
			}
			var list = data.result.list;
			if (!list || list.length == 0) {
				$loadTip.html("查无记录");
				return;
			}
			
			$list.parseHtml({
				data: list,
				beforeNode: {
					root: function(ms) {// memberSpend
						ms.cssClass = ms.isChecked ? "cost" : "";
						ms.detailUrl = ms.isChecked ? (g_userDomain + "/order/detail?serialCode=" + ms.serialCode) : "javascript:;";
						var date = new Date();
						date.setTime(ms.spendTime);
						ms.spendTime = date.format("yyyy-MM-dd hh:mm");
						ms.spend = ms.isChecked ?  ms.spend + "￥" : "未关联";
					}
				}
			});
			
			paging = data.result.paging;
			if (paging.nowPage == paging.totalPage) {
				$loadTip.hide();
			}
			
		});
	}
});